package com.proton.carepatchtemp.listener;

import android.os.SystemClock;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.utils.NetUtils;
import com.proton.carepatchtemp.utils.ThreadUtils;
import com.proton.carepatchtemp.utils.net.XMPPCenter;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.offline.OfflineMessageManager;

public class TaxiListener implements ConnectionListener {

    @Override
    public void connected(XMPPConnection connection) {
        LogUtils.d("XMPP connected");
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {
        LogUtils.d("XMPP authenticated");

        Presence unava = new Presence(Presence.Type.unavailable);
        unava.setMode(Presence.Mode.available);
        try {
            connection.sendStanza(unava);
        } catch (SmackException.NotConnectedException | InterruptedException e) {
            LogUtils.e(e);
        }
        //删除离线消息
        OfflineMessageManager offlineManager = new OfflineMessageManager(
                connection);
        try {
            if (offlineManager.getMessageCount() > 0) {
                offlineManager.deleteMessages();
            }
        } catch (Throwable e) {
            LogUtils.e(e);
        }

        Presence presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);

        ThreadUtils.runOnOtherThread(() -> {
            try {
                while (VCare.get().isAuthflag() && VCare.get().mXmpp != null) {
                    LogUtils.d("XMPPConnection online message");
                    if (VCare.get().getXmppConnection() != null) {
                        VCare.get().getXmppConnection().sendStanza(presence);
                    }
                    SystemClock.sleep(60 * 1000);
                }
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        XMPPCenter.onMessage();
    }

    @Override
    public void connectionClosed() {
        if (VCare.get().isAuthflag()) {
            XMPPCenter.loginChat();
        }
        LogUtils.d("XMPP connectionClosed");
    }

    @Override
    public void connectionClosedOnError(Exception e) {
        LogUtils.e(e);
        boolean error = e.getMessage().equals("stream:error (conflict)");

        if (!error) {
        }
        if (NetUtils.hasNet() && VCare.get().isAuthflag()) {
            XMPPTCPConnection conn = VCare.get().getXmppConnection();
            if (conn != null) {
                conn.disconnect();
                VCare.get().closeConnection();
                VCare.get().mXmpp = null;
            }
            VCare.get().mXmpp = null;
        }

        XMPPCenter.loginChat();
        LogUtils.e("XMPP connectionClosedOnError");
    }

    @Override
    public void reconnectingIn(int arg0) {
        LogUtils.d("XMPP reconnectingIn arg0 = " + arg0);
    }

    @Override
    public void reconnectionFailed(Exception arg0) {
        LogUtils.e(arg0);
        LogUtils.e("XMPP reconnectionFailed");
    }

    @Override
    public void reconnectionSuccessful() {
        LogUtils.d("XMPP reconnectionSuccessful");
        XMPPCenter.loginChat();
    }
}

